package threadpool;

/**
 * ClassName: DeadlockDemo
 * Package: threadpool
 *
 * @Author 马学兴
 * @Create 2024/3/16 20:24
 * @Version 1.0
 * Description:死锁的例子
 */
public class DeadlockDemo {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o1) {
                    System.out.println("获取锁1");
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o2){
                    System.out.println("获取锁2");
                }

            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o2) {
                    System.out.println("获取锁2");
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o1) {
                    System.out.println("获取锁1");
                }

            }
        });
        thread1.start();
        thread2.start();

    }

}
